import request from '@/utils/request'
import * as mock from './mock/security'

const isMock = import.meta.env.VITE_API_MOCK === 'true'

/**
 * 验证交易密码
 * @param {Object} data 密码数据
 * @param {string} data.password - 交易密码
 * @returns {Promise}
 */
export function verifyTransactionPassword(data) {
  if (isMock) {
    return Promise.resolve(mock.verifyTransactionPassword(data))
  }
  return request({
    url: '/security/verify-transaction-password',
    method: 'post',
    data
  })
}

/**
 * 设置交易密码
 * @param {Object} data 密码数据
 * @param {string} data.password - 交易密码
 * @param {string} data.confirmPassword - 确认交易密码
 * @returns {Promise}
 */
export function setTransactionPassword(data) {
  if (isMock) {
    return Promise.resolve(mock.setTransactionPassword(data))
  }
  return request({
    url: '/security/set-transaction-password',
    method: 'post',
    data
  })
}

/**
 * 修改交易密码
 * @param {Object} data 密码数据
 * @param {string} data.oldPassword - 原交易密码
 * @param {string} data.newPassword - 新交易密码
 * @param {string} data.confirmPassword - 确认新交易密码
 * @returns {Promise}
 */
export function updateTransactionPassword(data) {
  if (isMock) {
    return Promise.resolve(mock.updateTransactionPassword(data))
  }
  return request({
    url: '/security/update-transaction-password',
    method: 'post',
    data
  })
}

/**
 * 重置交易密码
 * @param {Object} data 密码数据
 * @param {string} data.verificationCode - 验证码
 * @param {string} data.newPassword - 新交易密码
 * @param {string} data.confirmPassword - 确认新交易密码
 * @returns {Promise}
 */
export function resetTransactionPassword(data) {
  if (isMock) {
    return Promise.resolve(mock.resetTransactionPassword(data))
  }
  return request({
    url: '/security/reset-transaction-password',
    method: 'post',
    data
  })
}

/**
 * 发送验证码
 * @param {Object} data 验证码数据
 * @param {string} data.type - 验证码类型 (email/phone)
 * @param {string} data.target - 接收验证码的邮箱或手机号
 * @returns {Promise}
 */
export function sendVerificationCode(data) {
  if (isMock) {
    return Promise.resolve(mock.sendVerificationCode(data))
  }
  return request({
    url: '/security/send-verification-code',
    method: 'post',
    data
  })
}

/**
 * 验证验证码
 * @param {Object} data 验证码数据
 * @param {string} data.type - 验证码类型 (email/phone)
 * @param {string} data.target - 接收验证码的邮箱或手机号
 * @param {string} data.code - 验证码
 * @returns {Promise}
 */
export function verifyCode(data) {
  if (isMock) {
    return Promise.resolve(mock.verifyCode(data))
  }
  return request({
    url: '/security/verify-code',
    method: 'post',
    data
  })
}